﻿using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace SharedClassLibrary.HttpRequest
{
    /// <summary>
    /// Http请求记录请求参数
    /// </summary>
    public class ApiClientLoggingDelegatingHandler : DelegatingHandler
    {
        private readonly ApiClientOptions options;

        public ApiClientLoggingDelegatingHandler(ApiClientOptions options)
        {
            this.options = options;
        }

        protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // 获取查询参数
            var queryParameters = request.RequestUri?.Query;

            // 获取 URL 中的路由参数 (如果有的话)
            var pathParameters = request.RequestUri?.AbsolutePath;

            // 获取请求体内容 (仅限 POST, PUT 请求)
            string requestBody = null;
            if (request.Method == HttpMethod.Post || request.Method == HttpMethod.Put)
            {
                if (request.Content != null)
                {
                    requestBody = await request.Content.ReadAsStringAsync();
                }
            }

            // 打印日志（根据需要）
            Console.WriteLine($"Request URI: {request.RequestUri}");
            Console.WriteLine($"Query Parameters: {queryParameters}");
            Console.WriteLine($"Path Parameters: {pathParameters}");
            Console.WriteLine($"Request Body: {requestBody}");
            return await base.SendAsync(request, cancellationToken);
        }
    }
}
